package cn.rwklyd.ecommerce.controller;

import cn.rwklyd.ecommerce.pojo.*;
import cn.rwklyd.ecommerce.service.AdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 键盘书生
 * @date 2024/12/3
 */
@RestController
@Slf4j
@RequestMapping("/admin")
public class adminController {
    @Autowired
    private AdminService adminService;

    //    查询商品信息
    @GetMapping("/productList")
    public Result productList(@RequestParam int page, @RequestParam int size) {
        PageHelper.startPage(page, size);
        List<Product> productList = adminService.productList();
        PageInfo<Product> pageInfo = new PageInfo<>(productList);
        return Result.success(pageInfo);
    }

    //    增加商品信息
    @PostMapping("/addProduct")
    public Result addProduct(@RequestBody Product product) {
        boolean b = adminService.addProduct(product);
        return b ? Result.success("商品信息添加成功") : Result.error("商品信息添加失败");
    }

    //    删除商品信息
    @GetMapping("/deleteProduct")
    public Result deleteProduct(@RequestParam String productId) {
        adminService.deleteProduct(productId);
        return Result.success();
    }

    //    修改商品信息
    @PostMapping("/updateProduct")
    public Result updateProduct(@RequestBody Product product) {
        adminService.updateProduct(product);
        return Result.success();
    }

    //    查询所有订单信息   //查询已经出售的商品信息
    @GetMapping("/queryOrderList")
    public Result queryOrderList() {
        List<Cart> cartList = adminService.queryOrderList();
        return Result.success(cartList);
    }

    //    查询顾客信息
    @GetMapping("/queryUserList")
    public Result queryUserList() {
        List<Users> users = adminService.queryUserList();
        return Result.success(users);
    }

    //    根据用户id查询用户的日志
    @GetMapping("/queryUserLog")
//    public Result queryUserLog(String id) {
//        List<LogAnalysis> userLogs = adminService.queryUserLog(id);
//        return Result.success(userLogs);
//    }
    public Result queryUserLog(String id, int page, int size) {
        PageHelper.startPage(page, size);
        List<LogAnalysis> userLogs = adminService.queryUserLog(id);
        PageInfo<LogAnalysis> pageInfo = new PageInfo<>(userLogs);
        return Result.success(pageInfo);
    }

}
